package com.shanzmoo.admin.web.store;

import cn.hutool.core.collection.CollUtil;
import com.shanzmoo.admin.domain.ExeclExportSet;
import com.shanzmoo.admin.domain.dto.StoreStockCheckBillDetailUpdateDTO;
import com.shanzmoo.admin.domain.dto.StoreStockCheckBillUpdateDTO;
import com.shanzmoo.admin.manager.store.StoreStockCheckBillDetailManager;
import com.shanzmoo.admin.manager.store.StoreStockCheckBillManager;
import com.shanzmoo.admin.web.BaseController;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.db.store.entity.StoreStockCheckBillDetailEntity;
import com.shanzmoo.db.store.entity.StoreStockCheckBillEntity;
import com.shanzmoo.db.store.entity.query.StoreStockCheckBillQuery;
import com.shanzmoo.db.store.entity.vo.StoreStockCheckBillVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;

/**
 * 库存盘点单 控制层
 *
 * @author fs 2021-03-09
 */
@Api(tags = "库存盘点单")
@RestController
@RequestMapping("/store/stock-check-bill")
public class StoreStockCheckBillController extends BaseController {

    @Autowired
    private StoreStockCheckBillManager storeStockCheckBillManager;
    @Autowired
    private StoreStockCheckBillDetailManager storeStockCheckBillDetailManager;

    @ApiOperation(value = "分页列表")
    @PostMapping("/pageList")
    public SHttpResponse<PageRespBase<StoreStockCheckBillVO>> pageList(@Valid @RequestBody StoreStockCheckBillQuery query) {
        PageRespBase<StoreStockCheckBillVO> pageList = storeStockCheckBillManager.queryPage(query);
        return SHttpResponse.ok(pageList);
    }

    @ApiOperation(value = "取消盘点单")
    @PostMapping("/cancel")
    public SHttpResponse<Boolean> cancel(@Valid @RequestBody StoreStockCheckBillUpdateDTO updateDto) {
        storeStockCheckBillManager.cancel(updateDto.convertEntity());
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "查看明细")
    @GetMapping("/info")
    public SHttpResponse<StoreStockCheckBillVO> info(@RequestParam("id") Integer id) {
        return SHttpResponse.ok(storeStockCheckBillManager.info(id));
    }

    @ApiOperation(value = "审核盘点单")
    @PostMapping("/exmine")
    public SHttpResponse<Boolean> exmine(@Valid @RequestBody StoreStockCheckBillUpdateDTO updateDto) {
        storeStockCheckBillManager.exmine(updateDto.convertEntity());
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "盘点单录入")
    @PostMapping("/enter")
    public SHttpResponse<Boolean> enter(@Valid @RequestBody StoreStockCheckBillUpdateDTO updateDto) {
        StoreStockCheckBillEntity entity = updateDto.convertEntity();
        List<StoreStockCheckBillDetailEntity> details = new ArrayList<>();
        List<StoreStockCheckBillDetailUpdateDTO> tmps = updateDto.getDetails();
        for(StoreStockCheckBillDetailUpdateDTO tmp:tmps){
            details.add(tmp.convertEntity());
        }
        storeStockCheckBillManager.enter(entity,details);
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "盘点完成")
    @GetMapping("/complete")
    public SHttpResponse<Boolean> complete(
            @NotEmpty(message = "单据id不允许为空") @ApiParam(value = "单据id") @RequestParam(value = "id") Integer id,
            @ApiParam(value = "备注") @RequestParam(value = "remark", required = false) String remark) {
        storeStockCheckBillManager.complete(id, remark);
        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "导出")
    @GetMapping("/export")
    public void export(@NotEmpty(message = "单据id不允许为空") @ApiParam(value = "单据id") @RequestParam(value = "id") Integer id) {
        List<StoreStockCheckBillDetailEntity> list = storeStockCheckBillDetailManager.export(id);
        List<ExeclExportSet> set = CollUtil.newArrayList(
                ExeclExportSet.create("storeCode","仓库编码"),
                ExeclExportSet.create("sku","sku"),
                ExeclExportSet.create("sysSkuNum","系统数量"),
                ExeclExportSet.create("actualSkuNum","实际数量")
        );
        exportByBean(set,list,"盘点单导出");
    }
 }
